/*
 * Copyright 2009 Colin Prinn
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package net.recursv.motific.unittest;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;

import java.io.File;

/**
 * Created by: recursv
 * Date: 31/07/2009
 * Time: 5:18:27 PM
 */
public class UtWeaverTask extends Task {

    protected Path _classpath;

    protected File _input;

    protected File _output;

    /**
     * Runs the code generators.
     */
    public void execute() throws BuildException {
        Weaver weaver = new Weaver();
        try {
            weaver.setClasspath(_classpath.list());
            weaver.setInputFile(_input);
            weaver.setOutputFile(_output);
            weaver.execute();
        } catch (Weaver.WeaverException e) {
            throw new BuildException(e);
        }
    }

    /**
     * Adds a path element to the classpath
     *
     * @param path a path element
     */
    public void setBootClasspath(Path path) {
        addToClasspath(path);
    }

    /**
     * Adds a reference to a classpath defined elsewhere.
     *
     * @param r a reference to a classpath
     */
    public void setBootClasspathRef(Reference r) {
        addToClasspath((Path) r.getReferencedObject());

    }

    /**
     * Adds a path element to the classpath
     *
     * @param path a path element
     */
    public void setClasspath(Path path) {
        addToClasspath(path);
    }

    /**
     * Adds a reference to a classpath defined elsewhere.
     *
     * @param ref a reference to a classpath
     */
    public void setClasspathRef(Reference ref) {
        addToClasspath((Path) ref.getReferencedObject());
    }

    private void addToClasspath(Path path) {
        if (_classpath == null)
            _classpath = new Path(getProject());
        _classpath.append(path);
    }

    /**
     * Sets the source files
     *
     * @param input The source
     */
    public void setInput(File input) {
        _input = input;
    }

    /**
     * Sets the output destination
     *
     * @param output the destination
     */
    public void setOutput(File output) {
        _output = output;
    }
}
